class Solution(object):
    def parseTernary(self, expression):
        """
        :type expression: str
        :rtype: str
        """
        while len(expression) > 1:
            cond = expression[0]
            count = 0
            if cond == 'T':
                left = 2
                right = 2
                while right < len(expression):
                    if expression[right] == ':':
                        count += 1
                        if count == 1:
                            expression = expression[left: right]
                            break
                    else:
                        if expression[right] == '?':
                            count -= 1
                    right += 1
            else:
                left = 2
                while left < len(expression):
                    if expression[left] == ':':
                        count += 1
                        if count == 1:
                            expression = expression[left + 1:]
                            break
                    else:
                        if expression[left] == '?':
                            count -= 1
                    left += 1
        return expression


data = Solution()
expression = "T?F?F:5:3"
print(data.parseTernary(expression))